Thread: [C help] Function for Dynamic Memory Allocation, realloc() gives segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    1

    Question [C help] Function for Dynamic Memory Allocation, realloc() gives segmentation fault

    I wrote some code in order to get a file name from the user at the terminal. I tried using realloc but there is an error in memory management that I do not understand. I am not sure if I am incorrectly terminating the string, or if there is another error that I cannot see with my limited C experience. Could anyone instruct me on what errors I have made?

    Code:
    #include
    Code:
    <stdio.h>
    #include <stdlib.h>
    #define CHUNK 4
    
    char *file_name;
    
    char *get_file_name(void) {
    
         char *line = NULL;
         char *tmp = NULL;
         int char_input = 0;
         size_t size = 0, index = 0;
         size = CHUNK;
    
         printf("Please enter the file name: \n");
    
         while(char_input != '\n' && char_input != EOF) {
    
         char_input = getc(stdin);
    
              if(size <= index) {
                   size += CHUNK;
                   tmp = realloc(line, size);
                   if (!tmp) {
                   free(line);
                   line = NULL;
                   break;
                   }
              line = tmp;
              }
    
              line[index] = char_input;
              index++;
    
         }
    
         line[index] = '\0';
    
         return line;
    
         }
    
    int main(void) {
    
        file_name = get_file_name();
    
        printf("%s", file_name);
    
    
        return 0;
    }
    Last edited by Anthony Orona; 10-02-2016 at 05:04 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Don't initialize size to CHUNK! Leave it at zero so memory will be allocated for the first time in the realloc (with line set to NULL).

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    It's amazing how few people keep up with new C standards.
    getline automatically allocates.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char * line = NULL;
        size_t len = 0;
    
        while (getline(&line, &len, stdin) != -1) {
            printf("[%s]\n", line);
        }   
        return 0;
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bigearsbilly
    It's amazing how few people keep up with new C standards.
    getline automatically allocates.
    A quick search does not bring up getline in C11, not even in an annex as an optional feature, perhaps borrowing from POSIX getline as it looks like from your example. Could you quote exactly where C11 specifies getline? As it stands, my guess is that you are one of those people who do not keep up with the new C standard, but rather confused POSIX with standard C.
    Last edited by laserlight; 10-03-2016 at 05:21 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by laserlight View Post
    A quick search does not bring up getline in C11, not even in an annex as an optional feature, perhaps borrowing from POSIX getline as it looks like from your example. Could you quote exactly where C11 specifies getline? As it stands, my guess is that you are one of those people who do not keep up with the new C standard, but rather confused POSIX with standard C.
    Amazingly enough there is a standard getline, after a fashion. It's included in the TR 24731-2 extension which you activate by defining __STDC_WANT_LIB_EXT2__.
    My best code is written with the delete key.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Prelude
    Amazingly enough there is a standard getline, after a fashion. It's included in the TR 24731-2 extension which you activate by defining __STDC_WANT_LIB_EXT2__.
    Indeed "after a fashion" since such a technical report specifies a library extension rather than a new part of the C standard library, though I suppose they might get included at some point (as we've certainly seen in C++).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Have you quite finished?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. In conclusion, we shouldn't be suggesting getline without the appropriate caveats (e.g., POSIX standard, or other extension to the standard library) because it is not part of standard C, contrary to your claim.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc Segmentation Fault
    By workisnotfun in forum C Programming
    Replies: 14
    Last Post: 10-21-2012, 06:03 AM
  2. Replies: 4
    Last Post: 03-14-2012, 09:23 AM
  3. Dynamic memory allocation for structs passed to function
    By eXcellion in forum C Programming
    Replies: 18
    Last Post: 03-30-2011, 03:30 PM
  4. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  5. Dynamic Memory Allocation (Seg. Fault)
    By dr_jaymahdi in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2007, 02:02 PM

Tags for this Thread